<?php
/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

/**
 * Get an url from a media id
 *
 * @param integer docId
 * @param string lang - optionnal
 * 
 * @example {mediaUrl docId=13 lang="FR"}
 */
function smarty_function_mediaUrl($params, &$smarty)
{ 
	if(!isset($params["lang"]))
	{
		$lang = f_core_Context::getInstance()->getLang();
	}
	else
	{
		$lang = $params["lang"];
	}
	
	$documentProvider = f_document_Provider::getInstance();
	
	if(isset($params["docId"]) && $documentProvider->checkDocument($params["docId"], $lang))
	{
        $document = $documentProvider->getByUniqueId($params["docId"], $lang);

		if($document instanceof m_medias_documents_Media)
		{
            if(isset($params["fileName"]))
            {
                return $document->getFileUrl($params["fileName"]);
            }
            return $document->getFileUrl();
		}		
	}
	
	return "#";
}